package com.guit.hrms.controller;

import com.guit.hrms.po.Announcement;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.AnnouncementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 保存公告
     *
     * @param announcement 公告信息
     * @return 操作结果
     */
    @PostMapping
    public Result saveAnnouncements(@RequestBody Announcement announcement) {
//        Map<String,Object> map = ThreadLocalUtil.get();
//        String role = map.get("role").toString();

        if (announcement.getDeptId() != null
                && announcement.getDeptType() != null
                && announcement.getTitle() != null
                && announcement.getContent() != null
                && announcement.getPublishTime() != null) {
            Integer result = announcementService.saveAnnouncements(announcement);
            if (result == 0) {
                return Result.error("上传失败");
            } else {
                return Result.success("上传成功");
            }
        } else {
            return Result.error("请输入全部字段");
        }
    }

    /**
     * 查看
     *
     * @param deptId 部门id
     * @return 公告列表
     */
    @GetMapping
    public Result viewAnnouncements(Integer deptId, Integer deptType) {

        List<Announcement> announcementList = announcementService.viewAnnouncements(deptId, deptType);
        if (announcementList.isEmpty()) {
            return Result.error("该（类型）部门没有公告");
        } else {
            return Result.success(announcementList);
        }
    }

    /**
     * 更新公告
     *
     * @param announcement 公告信息
     * @return 操作结果
     */
    @PatchMapping
    public Result updateAnnouncements(@RequestBody Announcement announcement) {
        System.out.println();
        System.out.println();
        System.out.println(announcement.toString());
        if (announcement.getId() != null) {
            if (announcement.getDeptId() != null
                || announcement.getDeptType() != null
                || announcement.getTitle() != null
                || announcement.getContent() != null
                || announcement.getPublishTime() != null) {
                Integer result = announcementService.updateAnnouncements(announcement);
                if (result == 0) {
                    return Result.error("该部门不存在");
                } else {
                    return Result.success("更新成功");
                }
            } else {
                return Result.error("请输入要更新的内容");
            }
        } else {
            return Result.error("请输入要更新公告的id");
        }
    }

    /**
     * 删除公告
     *
     * @param ids 公告id列表
     * @return 操作结果
     */
    @DeleteMapping("/{ids}")
    public Result DeleteAnnouncements(@PathVariable List<Integer> ids) {
        if (ids.isEmpty()) {
            return Result.error("请输入要删除公告的id");
        } else {
            Integer result = announcementService.deleteAnnouncements(ids);
            if (result == 0) {
                return Result.error("删除失败，请确定公告的id是否正确");
            } else {
                return Result.success("删除成功");
            }
        }
    }
}
